﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class frm_cap_nhat_hang_ban : Form
    {
        public frm_cap_nhat_hang_ban()
        {
            InitializeComponent();

            set_form_event();
        }
        #region Members        
        Supplier m_supplier;
        DataCSQLDataContext m_dtct_supplier = new DataCSQLDataContext(new Controler().ConnectionString);
        e_form_mode m_formmode = e_form_mode.THEM_HANG_BAN;
        #endregion

        #region Public Methods
        public void Display(string str_id)
        {
            m_supplier = (from c in m_dtct_supplier.Suppliers
                          where c.SupplierID == str_id
                          select c).Single<Supplier>();
            m_formmode = e_form_mode.SUA_HANG_BAN;
            this.ShowDialog();
        }
         public void Display()
         {             
             m_formmode = e_form_mode.THEM_HANG_BAN;
             this.ShowDialog();
         }
        #endregion

        #region Data Structures
        enum e_form_mode
        {
            THEM_HANG_BAN = 1,
            SUA_HANG_BAN = 2,
            XEM_HANG_BAN = 3
        }
        #endregion

        #region Private Methods
        //Gán thông tin từ form vào object
        private void form_to_obj()
        {
            m_supplier.SupplierID = txt_ID.Text;
            m_supplier.CompanyName = txt_compantynam.Text;
            m_supplier.Address = txt_dia_chi.Text;
            m_supplier.BankId = Convert.ToString( cbo_ngan_hang.SelectedValue);
            m_supplier.AcountBank = txt_so_tai_khoa.Text;
            m_supplier.Logo = txt_link_logo.Text;
            m_supplier.SerialRegister = txt_so_seri.Text;
            m_supplier.NumberNip = Convert.ToInt32(txt_so_nip.Text);
            m_supplier.Regin = txt_so_regon.Text;
            m_supplier.TempleOrder = txt_mau_hoa_don.Text;
            m_supplier.AutoCreateOrder = txt_mau_hd.Text;
            m_supplier.LengthOfOrderId = (int) mun_do_dai_so_hd.Value;
            m_supplier.Description = txt_thong_tin.Text;
        }

        //Gán thông tin từ object lên form
        private void obj_to_form()
        {
            txt_ID.Text = m_supplier.SupplierID;
            txt_compantynam.Text = m_supplier.CompanyName;
            txt_dia_chi.Text = m_supplier.Address;
            cbo_ngan_hang.SelectedValue = m_supplier.BankId;
            txt_so_tai_khoa.Text = m_supplier.AcountBank;
            txt_link_logo.Text = m_supplier.Logo;
            txt_so_seri.Text = 	m_supplier.SerialRegister	;
            txt_so_nip.Text = m_supplier.NumberNip.ToString();
            txt_so_regon.Text = m_supplier.Regin;
            txt_mau_hoa_don.Text = m_supplier.TempleOrder;
            txt_mau_hd.Text = m_supplier.AutoCreateOrder;
            mun_do_dai_so_hd.Value = Convert.ToDecimal(m_supplier.LengthOfOrderId);
            txt_thong_tin.Text = m_supplier.Description;

        }
        //Insert dữ liệu vào vào database
        private void insert_obj_to_database()
        {
            if (!check_loi()) return;
            m_supplier = new Supplier();
            //b1: gán thông tin từ form vào obj
            form_to_obj();
            //b2: insert vào database
            m_dtct_supplier.Suppliers.InsertOnSubmit(m_supplier);
            m_dtct_supplier.SubmitChanges();
            MessageBox.Show("Cập nhận thành công");
            this.Close();
        }
        //Insert dữ liệu vào vào database
        private void update_obj_to_database()
        {
            //b1: gán thông tin từ form vào obj
            form_to_obj();
            //b2: insert vào database                      
            m_dtct_supplier.SubmitChanges();
            MessageBox.Show("Cập nhận thành công");
            this.Close();
        }

        private void load_cbo_ngan_hang()
        {
            DataCSQLDataContext m_combo = new DataCSQLDataContext(new Controler().ConnectionString);
            var k = from d in m_combo.Banks
                    select new { d.BankID, d.BankName };
            cbo_ngan_hang.DataSource = k;
            cbo_ngan_hang.DisplayMember = "BankName";
            cbo_ngan_hang.ValueMember = "BankID";
        }
        private void load_cbo_mau_hoa_don()
        {
            //DataCSQLDataContext m_combo = new DataCSQLDataContext(new Controler().connectionString);
            //var k = from d in m_combo.
            //        select new { d.BankID, d.BankName };
            //cbo_ngan_hang.DataSource = k;
            //cbo_ngan_hang.DisplayMember = "BankName";
            //cbo_ngan_hang.ValueMember = "BankID";
        }
        //Insert dữ liệu vào vào database
        private void lam_lai_obj_to_database()
        {

        }

        private bool check_loi()
        {            
            return true;
        }
        //Format các control trong form
        private void set_form_load()
        {
            load_cbo_ngan_hang();
            switch (m_formmode)
            {
                case e_form_mode.THEM_HANG_BAN:
                    break;
                case e_form_mode.SUA_HANG_BAN:
                    obj_to_form();
                    txt_ID.Enabled = false;
                    break;
                case e_form_mode.XEM_HANG_BAN:
                    obj_to_form();
                    btn_xoa.Visible = false;
                    txt_ID.Enabled = false;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Events
        private void set_form_event()
        {
            //btn_thoat.Click += new EventHandler(btn_thoat_Click);
            this.KeyDown += new KeyEventHandler(frm_cap_nhat_hang_ban_KeyDown);
            btn_thêm.Click += new EventHandler(btn_thêm_Click);
        }

        void btn_thêm_Click(object sender, EventArgs e)
        {
            try
            {
                switch (m_formmode)
                {
                    case e_form_mode.THEM_HANG_BAN:
                        insert_obj_to_database();
                        break;
                    case e_form_mode.SUA_HANG_BAN:
                        update_obj_to_database();
                        break;
                    case e_form_mode.XEM_HANG_BAN:
                        break;
                    default:
                        break;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void frm_cap_nhat_hang_ban_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {                                        
                    case Keys.Escape:
                        this.Close();
                        break;                    
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btn_thoat_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        private void frm_cap_nhat_hang_ban_Load(object sender, EventArgs e)
        {
            set_form_load();
        }
        #endregion

     
    }
}
